package com.bhs.sansonglogistics.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.TypeBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.RefuseAdapter;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 拒绝原因
 */
public class BottomRefuseDialog extends BottomPopupView implements NetCallBack {


    private RecyclerView mRvList;
    private Button mBtnConfirm;
    private EditText mEtReason;

    private BaseFragment baseFragment;
    private String deliver_sn;
    List<TypeBean> list = new ArrayList<>();
    private BaseActivity baseActivity;

    public BottomRefuseDialog(@NonNull BaseFragment baseFragment, BaseActivity baseActivity, String deliver_sn) {
        super(baseFragment != null ? baseFragment.getContext() : baseActivity.mContext);
        this.baseActivity = baseActivity;
        this.baseFragment = baseFragment;
        this.deliver_sn = deliver_sn;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();

        list.add(new TypeBean("运费支付金额不符", 0));
        list.add(new TypeBean("无法安排车辆提货", 0));
        list.add(new TypeBean("专线停运", 0));
        list.add(new TypeBean("异形件无法托运", 0));
        list.add(new TypeBean("不支持到付", 0));
        list.add(new TypeBean("其他", 0));
        RefuseAdapter adapter = new RefuseAdapter(R.layout.item_refuse, list);
        mRvList.setAdapter(adapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (TypeBean typeBean : list) {
                    typeBean.setChecked(false);
                }
                list.get(position).setChecked(true);
                adapter.notifyDataSetChanged();
                if (list.get(position).getTypeName().equals("其他")) {
                    mEtReason.setVisibility(VISIBLE);
                } else {
                    mEtReason.setVisibility(GONE);
                }
            }
        });
    }

    private void initView() {
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mRvList = findViewById(R.id.rv_list);
        mBtnConfirm = findViewById(R.id.btn_confirm);
        mEtReason = findViewById(R.id.et_reason);
        mBtnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String refuse = null;
                for (TypeBean typeBean : list) {
                    if (typeBean.isChecked()) {
                        refuse = typeBean.getTypeName();
                    }
                }
                if (mEtReason.getVisibility() == View.VISIBLE) {
                    if (TextUtils.isEmpty(mEtReason.getText())) {
                        ToastUtil.show("请输入其他原因");
                        return;
                    }
                    refuse = mEtReason.getText().toString();
                }
                if (baseFragment != null) {
                    baseFragment.networkRequest(baseFragment.netApi.refuse(deliver_sn, refuse), BottomRefuseDialog.this);
                } else {
                    baseActivity.networkRequest(baseActivity.netApi.refuse(deliver_sn, refuse), BottomRefuseDialog.this);
                }
            }
        });

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_bottom_refuse;
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean dataBean = new Gson().fromJson(json, BaseBean.class);
        if (dataBean.isStatus()) {
            dismiss();
            ToastUtil.show("提交成功");
            if (listener != null) {
                listener.onClick(null);
            }
        } else {
            ToastUtil.show(dataBean.getMsg());
        }
    }

    private MyClickListener listener;

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }

    @Override
    public void onFailure(int requestCode) {

    }
}
